#include <iobuf.h>
#include <ts7200.h>

void IobufInit(iobuf *ib) {
  ib->front = ib->back = ib->size = 0;
}

int IobufEmpty(iobuf *ib) {
  return ib->size == 0;
}

int IobufFull(iobuf *ib) {
  return ib->size == IO_BUF_SIZE;
}

int IobufSize(iobuf *ib) {
  return ib->size;
}

int IobufEnqueue(iobuf *ib, int c) {
  if (ib->size == IO_BUF_SIZE) return 0;
  ib->buf[ib->back] = c;
  ib->back++; if (ib->back == IO_BUF_SIZE) ib->back = 0;
  ib->size++;
  return 1;
}

int IobufDequeue(iobuf *ib, int* out) {
  if (ib->size == 0) return 0;
  *out = ib->buf[ib->front];
  ib->front++; if (ib->front == IO_BUF_SIZE) ib->front = 0;
  ib->size--;
  return 1;
}
